/**
 * Created by chenzhicheng on 16/1/26.
 */

$(function() {

    $(".new-kind .good-list ul li,.new-kind2 .good-list ul li").hover(
        function(){
            $(this).children('.fruit-kinds .good-list ul li .wrap').stop().animate({"top": 0}, 500);
            $(this).addClass("cur");
        },
        function(){
            if ($(this).hasClass('first')) {
                $(this).children('.fruit-kinds .good-list ul li .wrap').stop().animate({"top": 637}, 500);
            }
            else {
                $(this).children('.fruit-kinds .good-list ul li .wrap').stop().animate({"top": 322}, 500);
            }
            $(this).addClass("cur");
        }
    );

    $(".new-kind .good-list ul li,.new-kind2 .good-list ul li").mouseleave(
        function(){
            $(this).removeClass("cur");
        }
    );

    $(".import-fruit .good-list ul li,.china-made .good-list ul li,.gift-card .good-list ul li").hover(
        function(){
            $(this).addClass("cur");
        }
    );

    $(".import-fruit .good-list ul li,.china-made .good-list ul li,.gift-card .good-list ul li").mouseleave(
        function(){
            $(this).removeClass("cur");
        }
    );


    $('.fruit-kinds .good-list ul li .wrap a span').click(function () {
            //$('.zhezhao').fadeIn(800);
            //$('.shop-cart').fadeIn(800);
            return false;
        }
    );

    $('.fruit-kinds .new-kind .good-list ul li .wrap a span,.fruit-kinds .new-kind2 .good-list ul li .wrap a span').click(function(){

            var pid = $('.fruit-kinds .good-list ul li.cur').attr('pid');
            var ppid = $('.fruit-kinds .good-list ul li.cur').attr('ppid');
            var pno = $('.fruit-kinds .good-list ul li.cur').attr('pno');
            var type = $('.fruit-kinds .good-list ul li.cur').attr('type');

            //加入购物车
            addCart(pid,ppid,pno,type);
        }
    );

    $('.f-list .leftpart .p-operate .s-che,.fruit-kinds .good-list ul li .s-info .s-che,.qx-content .qx-cor .qx-cont .qx-info .cart').click(
        function(){
            var li = $(this).parent().parent();
            $(this).animate({backgroundPosition:"-514px -291px"}, 500);
            var pid = li.attr('pid');
            var ppid = li.attr('ppid');
            var pno = li.attr('pno');
            var type = li.attr('type');


            //加入购物车
            addCart(pid,ppid,pno,type);
        }
    );

    $('.shop-cart .shop-top .cha').click(
        function(){
            $('.shop-cart').fadeOut(800);
            $('.zhezhao').fadeOut(800);
            $('.f-list .leftpart .p-operate .s-che,.fruit-kinds .good-list ul li .s-info .s-che,.qx-content .qx-cor .qx-cont .qx-info .cart').animate({backgroundPosition: "-517px -243px"}, 500);
        }
    );

});

/*
 * 加入购物车 - 继续购物
 */
function closeCart() {
    $('.shop-cart').fadeOut(800);
    $('.zhezhao').fadeOut(800);
    $('.f-list .leftpart .p-operate .s-che,.fruit-kinds .good-list ul li .s-info .s-che,.qx-content .qx-cor .qx-cont .qx-info .cart').animate({backgroundPosition: "-517px -243px"}, 500);
}

/*
 * 加入购物车
 */
function addCart(pid,ppid,pno,type)
{
    var items = {
        0: {
            'pid':pid,
            'ppid':ppid,
            'pno':pno,
            'type':type,
            'qty':1
        }
    };

    //添加商品到购物车
    $.ajax({
        type: 'POST',
        url: '/ajax/cart/add',
        dataType: 'json',
        data: {
            items:items
        },
        success: function(rs) {
            if (rs.code == 200) {
                $('.zhezhao').fadeIn(800);
                $('.shop-cart').fadeIn(800);

                var ds = rs.data;
                var cartcount = ds.cartcount;
                var cartprice = '￥'+ds.cart.total_amount;

                $(".cartcount").html(cartcount);
                $(".cartprice").html(cartprice);

                var cart_good = ds.cart;
                var is_gift = 1;

                for(var g_key in cart_good)
                {
                    if(g_key == 'pmt_alert')
                    {
                        is_gift =0;
                    }
                }

                if(is_gift == 0)
                {
//                        var cart_m_good = ds.cart.pmt_alert[0].solution;
//                        var pmt_id = cart_m_good.pmt_id;
//
//                        $(".cart_m_text").html(cart_m_good.title);
//                        if(cart_m_good.product_photo != '')
//                        {
//                            $("#cart_m_img").attr("src",cart_m_good.product_photo);
//                        }
                }
                else
                {
                    var cart_g_good = ds.cart.items;

                    for(var m_key in cart_g_good){
                        var str = m_key;
                        if(str.indexOf("gift") > 0)
                        {
                            $(".cart_m_text").html(cart_g_good[m_key].name);
                            $("#cart_m_img").attr("src",cart_g_good[m_key].photo.thum);
                        }
                    }
                }

            }
            else if(rs.code == 300){
                gdialog_info(rs.msg);
                $('.f-list .leftpart .p-operate .s-che,.fruit-kinds .good-list ul li .s-info .s-che,.qx-content .qx-cor .qx-cont .qx-info .cart').animate({backgroundPosition: "-517px -243px"}, 500);
            }

            var url = "http://"+window.location.host;
            $('.p-common-minicart').load(url+'/cart/resminicart' + " .p-common-minicart>*");
        }
    });
}